#!/usr/bin/perl
# Version 0.2.1

$testcase = "rtrlsa_bits";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 2) {
        print("Usage: $testcase.pl dump_file1 dump_file2 log_file");
        exit 1;
}

$dumpfile[0] = $ARGV[0];
$dumpfile[1] = $ARGV[1];
$logfile = $ARGV[2];

#------------------------------------------------------------------------------

$verdict_1 = 3; # Part A
$verdict_2 = 3; # Part B
$verdict_3 = 3; # Part C
$verdict_4 = 3; # Part D
$verdict_5 = 3; # Part E

# Open the first dumpfile
$success = 1;
open(DUMPFILE, $dumpfile[0]) or $success = 0;

if ($success eq 1) {
	$state_1 = 0;
	$state_2 = 0;
	while(<DUMPFILE>) {
		# Look for packets originated by 0.0.0.37
		if( (($_ =~ /0\.0\.0\.37 >/) and ($_ !~ /rtrid/ )) or ($_ =~ /rtrid 0\.0\.0\.37/)) {
			# only look at ls_upd packets 
			if( $_ =~ /OSPFv2-ls_upd/ ) {
				# look at area # 2 packets
				if( $_ =~ /area 0\.0\.0\.2/ ) {
					# router LSA for 0.0.0.37 should have the B-bit set
					# no E-bit
					if( $_ =~ /rtr 0\.0\.0\.37 (.*) E/ ) {
						# e-bit set - very bad!
						$state_1 = $state_1 | 1;
					}
			        if( $_ !~ /rtr 0\.0\.0\.37 B/) {
			        	# B-bit not set - bad
			        	$state_2 = $state_2 | 1;
			        }
				} else {
					# the other are's packets (0.0.0.0 and 0.0.0.1)
				
					# router LSA for 0.0.0.37 should have the B-bit and E-bit set
					if( $_ !~ /rtr 0\.0\.0\.37 (.*) E/ ) {
						# e-bit not set - very bad!
						$state_1 = $state_1 | 2;
					}
			        if( $_ !~ /rtr 0\.0\.0\.37 B/) {
			        	# B-bit not set - bad
			        	$state_2 = $state_2 | 2;
			        }
				}
			}
		}	
	}
	if($state_1 ne 0) {
		$verdict_1 = 2;
	} else {
		$verdict_1 = 1;
	}
	if($state_2 ne 0) {
		$verdict_2 = 2;
	} else {
		$verdict_2 = 1;
	}
}

# Open the second dumpfile
$success = 1;
open(DUMPFILE, $dumpfile[1]) or $success = 0;

if ($success eq 1) {
	
	$verdict_4 = 1;
	
	while(<DUMPFILE>) {
		# Look for packets originated by 0.0.0.5
		if( (($_ =~ /0\.0\.0\.5 >/) and ($_ !~ /rtrid/ )) or ($_ =~ /rtrid 0\.0\.0\.5/)) {
			# only look at ls_upd packets 
			if( $_ =~ /OSPFv2-ls_upd/ ) {
				
				# router LSA for 0.0.0.5 should have no E-bit set
				if( $_ =~ /rtr 0\.0\.0\.5 (.*) E/ ) {
					# e-bit set - very bad!
					$verdict_4 = 2;
				}
			}
		}	
	}
	
}

# Create report 

# feature not implemented yet.
$verdict_1 = 3;
if($verdict_1 eq 1) {
	print("E-bit set correctly in router LSAs of ASBR. Test PASSED.\n");
} elsif($verdict_1 eq 2) {
	print("E-bit is not set correctly in router LSAs of ASBR. Test FAILED.\n");
} else {
	print("Cannot determine the setting of the E-bit. Test INCONCLUSIVE.\n");
}

if($verdict_2 eq 1) {
	print("B-bit set correctly in router LSAs of ASBR. Test PASSED.\n");
} elsif($verdict_2 eq 2) {
	print("B-bit is not set correctly in router LSAs of ASBR. Test FAILED.\n");
} else {
	print("Cannot determine the setting of the B-bit. Test INCONCLUSIVE.\n");
}

# feature not impl. yet in OSPF
$verdict_3 = 3;
if($verdict_3 eq 1) {
	print("Bits set correctly in router LSAs of ASBR. Test PASSED.\n");
} elsif($verdict_3 eq 2) {
	print("Bits are not set correctly in router LSAs of ASBR. Test FAILED.\n");
} else {
	print("Cannot determine the setting of the bits. Test INCONCLUSIVE.\n");
}

if($verdict_4 eq 1) {
	print("E-bit set correctly in router LSAs of ASBR. Test PASSED.\n");
} elsif($verdict_4 eq 2) {
	print("E-bit is not set correctly in router LSAs of ASBR. Test FAILED.\n");
} else {
	print("Cannot determine the setting of the E-bit. Test INCONCLUSIVE.\n");
}

if($verdict_5 eq 1) {
	print("Bits set correctly in router LSAs of ASBR. Test PASSED.\n");
} elsif($verdict_5 eq 2) {
	print("Bits are not set correctly in router LSAs of ASBR. Test FAILED.\n");
} else {
	print("Cannot determine the setting of the bits. Test INCONCLUSIVE.\n");
}

$verdict = $verdict_1;
if ($verdict_2 ne $verdict) {
	if($verdict_2 eq 2) {
		$verdict = 2;
	} elsif ($verdict_2 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($verdict_3 ne $verdict) {
	if($verdict_3 eq 2) {
		$verdict = 2;
	} elsif ($verdict_3 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($verdict_4 ne $verdict) {
	if($verdict_4 eq 2) {
		$verdict = 2;
	} elsif ($verdict_4 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}
if ($verdict_5 ne $verdict) {
	if($verdict_5 eq 2) {
		$verdict = 2;
	} elsif ($verdict_5 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}




#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");